@file:JvmMultifileClass
@file:JvmName("DrawableKsl")

package hos.ksl

import android.content.res.ColorStateList
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Build
import androidx.annotation.ColorInt
import androidx.annotation.Px

/**
 * <p>Title: DrawableKsl </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2024-08-12 9:09
 * @version : 1.0
 */

fun createBorder(
    @ColorInt color: Int,
    @Px width: Int = 2,
    @Px dashWidth: Float = 0f,
    @Px dashGap: Float = 0f,
    @Px leftTopRadius: Float = 0f,
    @Px rightTopRadius: Float = 0f,
    @Px leftBottomRadius: Float = 0f,
    @Px rightBottomRadius: Float = 0f,
    @Px leftPadding: Int = 0,
    @Px topPadding: Int = 0,
    @Px rightPadding: Int = 0,
    @Px bottomPadding: Int = 0,
) {
    GradientDrawable().border(
        color,
        width, dashWidth, dashGap,
        leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius,
        leftPadding, topPadding, rightPadding, bottomPadding
    )
}

fun Drawable.copy(): Drawable = constantState?.newDrawable() ?: this

@JvmOverloads
fun GradientDrawable.padding(
    @Px left: Int = 0, @Px top: Int = 0, @Px right: Int = 0, @Px bottom: Int = 0
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        setPadding(left, top, right, bottom)
    } else {
        getPadding(Rect(left, top, right, bottom))
    }
    return this
}

@JvmOverloads
fun GradientDrawable.paddingHV(
    @Px horizontal: Int = 0,
    @Px vertical: Int = 0
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        setPadding(horizontal, vertical, horizontal, vertical)
    } else {
        getPadding(Rect(horizontal, vertical, horizontal, vertical))
    }
    return this
}

fun GradientDrawable.paddingAll(
    @Px padding: Int
) = paddingHV(padding, padding)

@JvmOverloads
fun GradientDrawable.radius(
    @Px leftTop: Float = 0f,
    @Px rightTop: Float = 0f,
    @Px leftBottom: Float = 0f,
    @Px rightBottom: Float = 0f
): GradientDrawable {
    setCornerRadii(
        floatArrayOf(
            leftTop, leftTop,
            rightTop, rightTop,
            rightBottom, rightBottom,
            leftBottom, leftBottom
        )
    )
    return this
}

@JvmOverloads
fun GradientDrawable.radiusHV(
    @Px left: Float = 0f,
    @Px right: Float = 0f
): GradientDrawable {
    return radius(left, right, left, right)
}

@JvmOverloads
fun GradientDrawable.radiusAll(
    @Px radius: Float = 0f,
): GradientDrawable {
    cornerRadius = radius
    return this
}

fun GradientDrawable.border(
    @ColorInt color: Int,
    @Px width: Int = 2,
    @Px dashWidth: Float = 0f,
    @Px dashGap: Float = 0f,
    @Px leftTopRadius: Float = 0f,
    @Px rightTopRadius: Float = 0f,
    @Px leftBottomRadius: Float = 0f,
    @Px rightBottomRadius: Float = 0f,
    @Px leftPadding: Int = 0,
    @Px topPadding: Int = 0,
    @Px rightPadding: Int = 0,
    @Px bottomPadding: Int = 0,
): GradientDrawable {
    setStroke(width, color, dashWidth, dashGap)
    radius(leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius)
    return padding(leftPadding, topPadding, rightPadding, bottomPadding)
}

@JvmOverloads
fun GradientDrawable.borderAll(
    @ColorInt color: Int,
    @Px width: Int = 2,
    @Px dashWidth: Float = 0f,
    @Px dashGap: Float = 0f,
    @Px radius: Float = 0f,
    @Px padding: Int = 0,
): GradientDrawable {
    setStroke(width, color, dashWidth, dashGap)
    radiusAll(radius)
    return paddingAll(padding)
}

@JvmOverloads
fun GradientDrawable.borderHV(
    @ColorInt color: Int,
    @Px width: Int = 2,
    @Px dashWidth: Float = 0f,
    @Px dashGap: Float = 0f,
    @Px leftRadius: Float = 0f,
    @Px rightRadius: Float = 0f,
    @Px horizontalPadding: Int = 0,
    @Px verticalPadding: Int = 0
): GradientDrawable {
    setStroke(width, color, dashWidth, dashGap)
    radiusHV(leftRadius, rightRadius)
    return paddingHV(horizontalPadding, verticalPadding)
}

@JvmOverloads
fun GradientDrawable.border(
    color: ColorStateList,
    @Px width: Int = 2,
    @Px dashWidth: Float = 0f,
    @Px dashGap: Float = 0f,
    @Px leftTopRadius: Float = 0f,
    @Px rightTopRadius: Float = 0f,
    @Px leftBottomRadius: Float = 0f,
    @Px rightBottomRadius: Float = 0f,
    @Px leftPadding: Int = 0,
    @Px topPadding: Int = 0,
    @Px rightPadding: Int = 0,
    @Px bottomPadding: Int = 0,
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setStroke(width, color, dashWidth, dashGap)
    } else {
        setStroke(width, color.defaultColor, dashWidth, dashGap)
    }
    radius(leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius)
    return padding(leftPadding, topPadding, rightPadding, bottomPadding)
}

@JvmOverloads
fun GradientDrawable.borderAll(
    color: ColorStateList,
    @Px width: Int = 2,
    @Px dashWidth: Float = 0f,
    @Px dashGap: Float = 0f,
    @Px radius: Float = 0f,
    @Px padding: Int = 0,
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setStroke(width, color, dashWidth, dashGap)
    } else {
        setStroke(width, color.defaultColor, dashWidth, dashGap)
    }
    radiusAll(radius)
    return paddingAll(padding)
}

@JvmOverloads
fun GradientDrawable.borderHV(
    color: ColorStateList,
    @Px width: Int = 2,
    @Px dashWidth: Float = 0f,
    @Px dashGap: Float = 0f,
    @Px leftRadius: Float = 0f,
    @Px rightRadius: Float = 0f,
    @Px horizontalPadding: Int = 0,
    @Px verticalPadding: Int = 0
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setStroke(width, color, dashWidth, dashGap)
    } else {
        setStroke(width, color.defaultColor, dashWidth, dashGap)
    }
    radiusHV(leftRadius, rightRadius)
    return paddingHV(horizontalPadding, verticalPadding)
}

fun GradientDrawable.background(
    @ColorInt color: Int,
    @Px leftTopRadius: Float = 0f,
    @Px rightTopRadius: Float = 0f,
    @Px leftBottomRadius: Float = 0f,
    @Px rightBottomRadius: Float = 0f,
    @Px leftPadding: Int = 0,
    @Px topPadding: Int = 0,
    @Px rightPadding: Int = 0,
    @Px bottomPadding: Int = 0,
): GradientDrawable {
    setColor(color)
    radius(leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius)
    return padding(leftPadding, topPadding, rightPadding, bottomPadding)
}


@JvmOverloads
fun GradientDrawable.backgroundAll(
    @ColorInt color: Int,
    @Px radius: Float = 0f,
    @Px padding: Int = 0,
): GradientDrawable {
    setColor(color)
    radiusAll(radius)
    return paddingAll(padding)
}

@JvmOverloads
fun GradientDrawable.backgroundHV(
    @ColorInt color: Int,
    @Px leftRadius: Float = 0f,
    @Px rightRadius: Float = 0f,
    @Px horizontalPadding: Int = 0,
    @Px verticalPadding: Int = 0
): GradientDrawable {
    setColor(color)
    radiusHV(leftRadius, rightRadius)
    return paddingHV(horizontalPadding, verticalPadding)
}

fun GradientDrawable.background(
    color: ColorStateList,
    @Px leftTopRadius: Float = 0f,
    @Px rightTopRadius: Float = 0f,
    @Px leftBottomRadius: Float = 0f,
    @Px rightBottomRadius: Float = 0f,
    @Px leftPadding: Int = 0,
    @Px topPadding: Int = 0,
    @Px rightPadding: Int = 0,
    @Px bottomPadding: Int = 0,
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setColor(color)
    } else {
        setColor(color.defaultColor)
    }
    radius(leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius)
    return padding(leftPadding, topPadding, rightPadding, bottomPadding)
}

@JvmOverloads
fun GradientDrawable.backgroundAll(
    color: ColorStateList,
    @Px radius: Float = 0f,
    @Px padding: Int = 0,
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setColor(color)
    } else {
        setColor(color.defaultColor)
    }
    radiusAll(radius)
    return paddingAll(padding)
}

@JvmOverloads
fun GradientDrawable.backgroundHV(
    color: ColorStateList,
    @Px leftRadius: Float = 0f,
    @Px rightRadius: Float = 0f,
    @Px horizontalPadding: Int = 0,
    @Px verticalPadding: Int = 0
): GradientDrawable {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setColor(color)
    } else {
        setColor(color.defaultColor)
    }
    radiusHV(leftRadius, rightRadius)
    return paddingHV(horizontalPadding, verticalPadding)
}

